﻿/**
 * 
 * Copyright 2009, 2010 Mike Jamer [email: mikejamer@gmail.com ]
 * 
 * This File is part of Moose IRC
 * 
 * MooseIRC is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * MooseIRC is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with MooseIRC.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

using System;
using moose.extension;

namespace moose.irc.standard {

    /// <summary>
    /// Connect to an IRC Server
    /// </summary>
    [CLSCompliant(true)]
    [Command(
        Name = "moose-cmd-connect",
        Description = "Request Core connect to IRC server",
        Author = "Mike MacKenzie-Jamer <email: mikejamer@gmail.com>",
        Version = "0.1")]
    [Trigger(TriggerText = "SERVER")]
    [Trigger(TriggerText = "CONNECT")]
    public sealed class ConnectCommand : IrcAction {

        protected override IrcState valid_states() {
            return IrcState.Disconnected;
        }

        protected override void action(IrcClient client, IrcMessage message) {

            //TODO: will need to provide better means of connecting, 
            // From here, will need to allow this command to do the dns resolving, and change
            // the IrcConnection to take endpoints as the parameter,
            // at least as an alternate way of connecting.

            // From there, we will use this to allow retrys, and use of alternate servers in
            // server groups.  ie, undernet, efnet or whatever...

            int port = 0;
            if (message.Arguments.Length < 2) {
                client.echo("Connect expecting at least 2 arguments, host/port...");
                return;
            }

            if (!int.TryParse(message.Arguments[1], out port)) {
                client.echo("Port must be numeric");
                return;
            }

            if (port < 1 || port > 65535) {
                client.echo("port must be between 1 and 65535");
                return;
            }

            try {
                client.echo("Connecting to: " + message.Arguments[0] + ":" + message.Arguments[1]);
                client.connect(message.Arguments[0], port);

                IrcState old_state = client.SessionState;
                client.SessionState = IrcState.Connected;

                IrcEvent state_event = new IrcEvent(IrcEvents.server_connected);
                client.notify_event(state_event);

            } catch (Exception) {
                client.echo("Failed to connect to: " + message.Arguments[0] + ":" + message.Arguments[1]);
                return;
            }

            return;
        }
    }
}
